
// XortStudioDoc.h : interface of the CXortStudioDoc class
//


#pragma once

#include "DisassemblerRTF.h"
class CXortStudioView;


class CXortStudioDoc : 
    public CRichEditDoc,
    public XRuntimeDebugEvents
{
protected: // create from serialization only
	CXortStudioDoc();
	DECLARE_DYNCREATE(CXortStudioDoc)

// Attributes
public:

// Operations
public:
    inline CXortStudioView * GetMainView() { return (CXortStudioView*)GetView(); }

    void DebugRun();
    void DebugStop();  
    void DebugStepInto();
    void DebugStepOver();
    void ToggleBreakpoint();

    void LocateSymbolInView( symbolref pSymbol );
    const CString& GetCmdLine() const { return m_csCmdLine; }
    void SetCmdLine( LPCSTR lpszCmdLine ) { m_csCmdLine = lpszCmdLine; }

    // function returns TRUE if bp was set or FALSE if it was removed
    BOOL SetBreakpoint( DWORD dwLineIdx );
    void LocateLineInView( DWORD dwLineIdx );

// Overrides
public:
	virtual BOOL OnNewDocument();
    virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
    virtual BOOL OnSaveDocument(LPCTSTR lpszPathName);
    virtual void OnCloseDocument();
    virtual BOOL IsModified() { return FALSE; }
	virtual void Serialize(CArchive& ar);
	virtual CRichEditCntrItem* CreateClientItem(REOBJECT* preo) const;
#ifdef SHARED_HANDLERS
	virtual void InitializeSearchContent();
	virtual void OnDrawThumbnail(CDC& dc, LPRECT lprcBounds);
#endif // SHARED_HANDLERS

// Implementation
public:
	virtual ~CXortStudioDoc();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

private:
    inline void     setState( XVMState newState ) { m_eState = newState; }
    inline XVMState getState() const { return m_eState; }

    static UINT __cdecl DebugThreadProcStatic( LPVOID pParam );
    void DebugThreadProc();
    void UpdateStackView();
    void WaitForDebugEvent();
    void OnProgramExecutionEnd();

    CString         m_csAsmRTF;
    SYMBOL_LIST     m_listGlobal;
    STRING_ARRAY    m_litPool;
    string          m_csFileName;
    XRuntime        m_runTime;
    CString         m_csCmdLine;
    DBGLINES_ARRAY  m_arLines;      // the asrray of information abot disasembled lines

    int             m_iArgc;
    char          **m_pArgv;

    CWinThread     *m_pDebugThread;     // thread where the debugged program persists.
    XVMState        m_eState;           // current program state
    XVMStepKind     m_NextStepKind;     // into || over || out - for next procedure

protected:
    virtual int onEnterSystemFrame( XRuntime *pRT, SymbolRef CurrentSymbol );
    virtual int onLeaveSystemFrame( XRuntime *pRT, SymbolRef CurrentSymbol );
    virtual int onEnterFrame( XRuntime *pRT );
    virtual int onLeaveFrame( XRuntime *pRT );
    virtual int beforeExecPrimitive( XRuntime *pRT, const InstructionData& primInfo );
    virtual int afterExecPrimitive( XRuntime *pRT, const InstructionData& primInfo, int iLastError );
    virtual int beforeExecBuildinProc( XRuntime *pRT );
    virtual int afterExecBuildinProc( XRuntime *pRT, int iLastError );
    virtual int beforeExecNativeProc( XRuntime *pRT );
    virtual int afterExecNativeProc( XRuntime *pRT, int iLastError );
    virtual int internalXVMError( XRuntime *pRT, int iError );
    virtual void outputDebugMessage( LPCSTR lpszMessage );

// Generated message map functions
protected:
	DECLARE_MESSAGE_MAP()

#ifdef SHARED_HANDLERS
	// Helper function that sets search content for a Search Handler
	void SetSearchContent(const CString& value);
#endif // SHARED_HANDLERS
};
